#include "NoteAddDialog.h"
#include "ui_NoteAddDialog.h"

NoteAddDialog::NoteAddDialog(Note* note, QWidget *parent) :
	QDialog(parent),
	ui(new Ui::NoteAddDialog),
	_note(note)
{
	ui->setupUi(this);

	ui->btnAdd->setEnabled(false);

	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	connect(ui->editUrl,SIGNAL(textChanged(QString)),SLOT(onDataChanged()));
	connect(ui->editPass,SIGNAL(textChanged(QString)),SLOT(onDataChanged()));
	connect(ui->editLogin,SIGNAL(textChanged(QString)),SLOT(onDataChanged()));
	connect(ui->editComment,SIGNAL(textChanged(QString)),SLOT(onDataChanged()));

	connect(ui->btnAdd,SIGNAL(clicked()),SLOT(onAddNote()));
	connect(ui->btnCancel,SIGNAL(clicked()),SLOT(close()));

	this->setWindowTitle(tr("Add new note"));
	if(_note != NULL){
		this->setWindowTitle(tr("Update note"));
		ui->btnAdd->setText(tr("Update"));
		ui->editUrl->setText(note->getUrl());
		ui->editLogin->setText(note->getLogin());
		ui->editPass->setText(note->getPass());
		ui->editComment->setText(note->getComment());
		onDataChanged();
	}
}

NoteAddDialog::~NoteAddDialog()
{
	delete ui;
}

Note* NoteAddDialog::getNote()
{
	return _note;
}

void NoteAddDialog::onAddNote()
{
	if(!_note) _note = new Note();
	_note->setUrl(ui->editUrl->text().toLocal8Bit());
	_note->setLogin(ui->editLogin->text().toLocal8Bit());
	_note->setPass(ui->editPass->text().toLocal8Bit());
	_note->setComment(ui->editComment->toPlainText().toLocal8Bit());

	close();
}

void NoteAddDialog::onDataChanged()
{
	bool state = !ui->editUrl->text().isEmpty() &&
			!ui->editPass->text().isEmpty() &&
			!ui->editLogin->text().isEmpty();

	ui->btnAdd->setEnabled(state);
}
